/**
 * 
 */
package neptune.manager.validator.rules.node;

import java.util.ArrayList;

import neptune.manager.model.topology.NetworkInterface;
import neptune.manager.model.topology.Node;
import neptune.manager.utils.Logger.MessageType;
import neptune.manager.validator.ValidatorResult;
import neptune.manager.validator.ValidatorRule;
import neptune.manager.validator.Validator.Status;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class NetInterfaceNameUniquenessRule extends ValidatorRule<Node> {

	@Override
	public String getName() {
		return "NetInterfaceNameUniquenessRule";
	}

	@Override
	protected ValidatorResult validationAlgorithm(Node toValidate,
			ValidatorResult result) {

		ArrayList<String> ifNames = new ArrayList<String>(0);

		for (NetworkInterface netIf : toValidate.getInterfaces()) {

			if (ifNames.contains(netIf.getInterfaceName())) {
				result.setStatus(Status.ERROR);
				result.getLog().addMessage(
						"Duplicated interface name '"
								+ netIf.getInterfaceName() + "'",
						MessageType.ERROR);
			} else
				ifNames.add(netIf.getInterfaceName());
		}
		return result;
	}

}
